package com.JingYan.aop;


import com.JingYan.domain.AjaxResult;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author DongJiaQi
 * @create 2022/7/29 13:47
 * @Description: WS代理
 *
 */
@Aspect
@Component
//切记不可提交！！！
public class WsAspect {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
/*
    @Pointcut("execution(* com.ysgl.thirdParty.service.impl.TransitionWsServiceImpl.sendAndRecv(..))")
    public void sendAndRecv(){}
    @Pointcut("execution(* com.ysgl.thirdParty.service.impl.InvokeServiceImpl.invokeEquipment*(..))")
    public void invokeEquipment(){} */




    /**
     * 针对 security 框架 ,调试方便,不可提交至生产
     * 代理密码校验,直接返回 true 。 省的问密码  直接跳过密码校验
     * 有其他校验的东西烦的可以同理干掉
     * 切记不可提交！！！
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */

    //@Around("sendAndRecv()")
    public AjaxResult sendAndRecvAround(ProceedingJoinPoint joinPoint) throws Throwable {
        logger.info("进入代理WS");
        //开启代理
        boolean aspectFlag = true;
        //获取方法参数值数组
        Object[] args = joinPoint.getArgs();
        //得到其方法签名
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //获取方法参数类型数组
        Class[] paramTypeArray = methodSignature.getParameterTypes();
        //动态修改其参数
        //注意，如果调用joinPoint.proceed()方法，则修改的参数值不会生效，必须调用joinPoint.proceed(Object[] args)
        if(!aspectFlag){
            logger.info("进入原方法");
            logger.info("参数 {}",JSON.toJSONString(args));
            AjaxResult result = (AjaxResult) joinPoint.proceed(args);
            return result;
        }
        logger.info("走代理方法");
        logger.info("参数 {}",JSON.toJSONString(args));
        //{"msg":"处方[TL890890.1000887371]已存在, 不能重复添加.","code":"0"}
        //如果这里不返回result，则目标对象实际返回值会被置为null
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put(AjaxResult.CODE_TAG,"1");
        ajaxResult.put(AjaxResult.DATA_TAG,"");
        ajaxResult.put(AjaxResult.MSG_TAG,"");
        String result = JSON.toJSONString(ajaxResult);
        return JSONObject.parseObject(result, AjaxResult.class);

    }

    //@Around("invokeEquipment()")
    public String invokeEquipmentAround(ProceedingJoinPoint joinPoint) throws Throwable {
        logger.info("进入代理Equipment");
        //开启代理
        boolean aspectFlag = false;
        //获取方法参数值数组
        Object[] args = joinPoint.getArgs();
        //得到其方法签名
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //获取方法参数类型数组
        Class[] paramTypeArray = methodSignature.getParameterTypes();



        //动态修改其参数
        //注意，如果调用joinPoint.proceed()方法，则修改的参数值不会生效，必须调用joinPoint.proceed(Object[] args)
        if(!aspectFlag){
            logger.info("进入原方法");
            logger.info("参数 {}",JSON.toJSONString(args));
            return (String) joinPoint.proceed(args);
        }
        logger.info("走代理方法");
        logger.info("参数 {}",JSON.toJSONString(args));
        //{"msg":"处方[TL890890.1000887371]已存在, 不能重复添加.","code":"0"}
        //如果这里不返回result，则目标对象实际返回值会被置为null
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put(AjaxResult.CODE_TAG,"1");
        ajaxResult.put(AjaxResult.DATA_TAG,"");
        ajaxResult.put(AjaxResult.MSG_TAG,"");
        String jsonString = JSON.toJSONString(ajaxResult);
        return jsonString;

    }



}
